<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of newForum
 *
 * @author ivan
 */
include_once 'function.php';


class newForum {
    //put your code here
    
    
    private $title;
    private $isPrivate;
    
    
    
    public function __construct($title,$isPrivate) {
        
        $title = htmlspecialchars($title);
        $isPrivate = htmlspecialchars($isPrivate);
        
        
        
        $this->title = $title;
        $this->isPrivate = $isPrivate;
        $this->Db = Db();
        
    }
    
    
    public function check(){
        
        if(strlen($this->title) < 1){
            
            $error = "Title length too short";
            return $error;
        }
        else{
            
            $value = $this->checkexistance();
            
            if($value==1)
                return "ok";
            else{
                
                $error = "A forum having this title already exists";
                return $error;
            }
        }
            
            
        }
        
        
        
        public function checkexistance(){
        
        $query = $this->Db->prepare('SELECT * FROM forum where forumTitle = :title');
        $query->execute(array(
            'title' => $this->title,
        ));
        
        $response = $query->fetch();
        
        if ($response == FALSE){
            
            return 1;
        }
        
        else{
            
            return -1;
        }
        
    }
        
    
    
    
    
    public function add(){
        
         $query = $this->Db->prepare('INSERT INTO forum(forumTitle,dateCreated,dateUpdated,numberOfTopics,numberOfReplies,owner,lastUpdatedBy,isPrivate) values(:title,CURDATE(),CURDATE(),0,0,:owner,:lastUpdate,:isPrivate)');
         $query->execute(array(
            'title' => $this->title,
             'owner' => $_SESSION['id'],
             'lastUpdate' => $_SESSION['id'],
             'isPrivate' => $this->isPrivate,
        ));
        
         
         
         
       
        return 1;
    }
}
